package dao.system;

import dao.BaseDaoHibernate;
import java.util.logging.Level;
import model.system.SystemNumbering;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import utility.StringUtils;


@Repository
public class SystemNumberingDao extends BaseDaoHibernate<SystemNumbering> {
    
    public String getNextNumberDb(int id, java.util.Date date) {
        Session session = sessionFactory.openSession();
        String nextNumber = "";
        try {
            session.beginTransaction();
            Query q = session.createSQLQuery("select get_next_number("+id+","+StringUtils.formatSqlDate(date) +")");
            nextNumber = (String) q.uniqueResult();
           
            session.getTransaction().commit();
        } catch (RuntimeException ex) {
            java.util.logging.Logger.getLogger(SystemNumberingDao.class.getName()).log(Level.SEVERE, null, ex);
            session.getTransaction().rollback(); 
        } finally {
            if (session != null) { session.close();}
            return nextNumber;
        }
    }
}
